package com.hu.hutool.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hu.hutool.entity.Dog;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @Author CazCen
 * @Date 2021/11/18 12:11
 */
@Slf4j
public class FastJsonTest {

    public static void JsonString() {
        Dog dog = new Dog();
        dog.setEyeNumber(2);
        dog.setFootNumber(4);
        dog.setMouseNumber(1);
        dog.setWeightNumber(50);
        Object o = JSON.toJSON(dog);
        String s = JSON.toJSONString(dog);
        log.info("o:{}", o);
        log.info("dog:{}", dog);
        log.info("s:{}", s);
        Dog dog1 = JSON.parseObject(s, Dog.class);
        log.info("dog1:{}", dog1);
    }


    public static void JsonObjectString() {
        Dog dog = new Dog();
        dog.setEyeNumber(2);
        dog.setFootNumber(4);
        dog.setMouseNumber(1);
        dog.setWeightNumber(50);
        String s = JSONObject.toJSONString(dog);
        Dog dog1 = JSONObject.parseObject(s, Dog.class);

        log.info("s:{}", s);
        log.info("dog1:{}", dog1);

    }


    public static void JsonArrayString() {
        Dog dog = new Dog();
        dog.setEyeNumber(2);
        dog.setFootNumber(4);
        dog.setMouseNumber(1);
        dog.setWeightNumber(50);

        Dog dog1 = new Dog();
        dog1.setEyeNumber(1);
        dog1.setFootNumber(1);
        dog1.setMouseNumber(1);
        dog1.setWeightNumber(1);

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(dog);
        jsonArray.add(dog1);

        log.info("jsonArray:{}", jsonArray);

        log.info("jsonArray.toString :{}", jsonArray.toString());

        List<Dog> dogs = jsonArray.toJavaList(Dog.class);


        List<Dog> dogs1 = JSON.parseObject(jsonArray.toString(), new TypeReference<List<Dog>>() {
        });

        log.info("dogs1:{}", dogs1);

    }

}
